home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / QuickTime™ VR 2.0 SDK / QTVR C⁄C++ Runtime API / Sample Code / VRShell Sample Code / VR3DSound / Application Files / MacApplication.c < prev    next >
Encoding:
Text File  |  1997-05-22  |  10.3 KB  |  466 lines  |  [TEXT/MPCC]

  1. //
  2. //    File:        MacApplication.c
  3. //
  4. //    Contains:    Localized sound support for QuickTime VR movies.
  5. //
  6. //    Written by:    Tim Monroe
  7. //                Based (heavily!) on the MovieShell code written by Apple DTS
  8. //
  9. //    Copyright:    © 1994-1996 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <5>         01/24/97    rtm        reworked InitApplicationWindowObject
  14. //       <4>         12/09/96    rtm        added 3D sound support (see TestFunctions.c)
  15. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  16. //       <2>         11/27/96    rtm        conversion to personal coding style;
  17. //                                    added preliminary QTVR support
  18. //       <1>         12/21/94    khs        first file
  19. //       
  20. //
  21. // TODO:
  22. // + should turn a window's sound off/on on suspend/resume and deactivate/activate events
  23.  
  24. // header files
  25. #include "MacApplication.h"
  26. #include "MacFramework.h"
  27. #include "AppConfiguration.h"
  28. #include "DTSQTUtilities.h"
  29. #include "QTVRUtilities.h"
  30. #include <Resources.h>
  31.  
  32. // Header file for the specific test functions.
  33. #include "TestFunctions.h"
  34.  
  35. // global variables
  36. long            gMaxMilliSecToUse = kMaxMilliSecToUse;
  37. Boolean            gQTVRMgrIsPresent = false;    // is the QuickTime VR Manager available?        
  38. Boolean            gHasSoundSprockets;            // is SoundSprockets available?
  39.  
  40. //////////
  41. //
  42. // InitApplication
  43. // Do any application-specific initialization.
  44. //
  45. //////////
  46.  
  47. void InitApplication (void)
  48. {
  49.     // make sure that the QTVR Manager is present in the present operating environment
  50.     if (QTVRUtils_IsQTVRMgrInstalled()) {
  51.         gQTVRMgrIsPresent = true;
  52.     } else {
  53.         ShowWarning("\pThe QuickTime VR Manager cannot be found.", 0);
  54.         ExitToShell();
  55.     }
  56.  
  57.     // initialize 3D sound capabilities
  58.     VR3DSound_Init();
  59. }
  60.  
  61.  
  62. //////////
  63. //
  64. // StopApplication
  65. // Do any application-specific shutdown.
  66. //
  67. //////////
  68.  
  69. void StopApplication (void)
  70. {
  71.  
  72.  
  73. }
  74.  
  75.  
  76. //////////
  77. //
  78. // DoIdle
  79. // Do any processing that can/should occur at idle time.
  80. //
  81. //////////
  82.  
  83. void DoIdle (WindowRef theWindow)
  84. {
  85.     GrafPtr             mySavedPort;
  86.     WindowObject         myWindowObject;
  87.     MovieController        myMC = NULL;
  88.     Point                myPoint;
  89.     
  90.     GetPort(&mySavedPort);
  91.     SetPort(theWindow);
  92.     
  93.     myWindowObject = (WindowObject)GetWRefCon(theWindow);
  94.     if (myWindowObject != NULL) {
  95.     
  96.         myMC = (**myWindowObject).fController;
  97.         if (myMC != NULL) {
  98.         
  99.             MoviesTask(MCGetMovie(myMC), gMaxMilliSecToUse);
  100.  
  101.             // restore the cursor to the arrow if it's outside the front movie window
  102.             // and the mouse button isn't still down;
  103.             // this is needed only for VR movies, but should probably always be done
  104.             if (theWindow == FrontWindow()) {
  105.                 GetMouse(&myPoint);
  106.                 if (!PtInMovie(MCGetMovie(myMC), myPoint) && !StillDown())
  107.                     InitCursor();
  108.             }
  109.         }
  110.     }
  111.  
  112.     // @@@INSERT ANY OTHER IDLE-BASED FUNCTIONALITY HERE
  113.     
  114.     SetPort(mySavedPort);
  115. }
  116.  
  117.  
  118. //////////
  119. //
  120. // DoUpdateWindow
  121. // Update the specified window.
  122. //
  123. //////////
  124.  
  125. void DoUpdateWindow (WindowRef theWindow, Rect *theRefreshArea)
  126. {
  127.     GrafPtr             mySavedPort;
  128.     
  129.     GetPort(&mySavedPort);
  130.     SetPort(theWindow);
  131.     
  132.     BeginUpdate(theWindow);
  133.  
  134.     EraseRect(theRefreshArea);        // this is important, for non-rectangular movies
  135.     
  136.     // @@@INSERT WINDOW-SPECIFIC DRAWING FUNCTIONALITY HERE
  137.  
  138.     EndUpdate(theWindow);
  139.     SetPort(mySavedPort);
  140. }
  141.  
  142.  
  143. //////////
  144. //
  145. // HandleContentClick
  146. // Handle mouse button clicks in the specified window.
  147. //
  148. //////////
  149.  
  150. void HandleContentClick (WindowRef theWindow, EventRecord *theEvent)
  151. {
  152. #pragma unused(theEvent)
  153.  
  154.     GrafPtr             mySavedPort;
  155.     
  156.     GetPort(&mySavedPort);
  157.     SetPort(theWindow);
  158.  
  159.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  160.  
  161.     SetPort(mySavedPort);
  162. }
  163.  
  164.  
  165. //////////
  166. //
  167. // HandleQTVRKeyPress
  168. // Handle QuickTime VR-specific key presses.
  169. // Returns true if the key press was handled, false otherwise.
  170. //
  171. //////////
  172.  
  173. Boolean HandleQTVRKeyPress (EventRecord *theEvent)
  174. {
  175.     Boolean        isHandled = true;
  176.     char        myCharCode;
  177.     
  178.     myCharCode = theEvent->message & charCodeMask;
  179.  
  180.     switch (myCharCode) {
  181.     
  182.         default:
  183.             isHandled = false;
  184.             break;
  185.     }
  186.  
  187.     return(isHandled);
  188. }
  189.  
  190.  
  191. //////////
  192. //
  193. // CreateMovieWindow
  194. // Create a window to display a movie in.
  195. //
  196. //////////
  197.  
  198. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  199. {
  200.     WindowRef            myWindow;
  201.         
  202.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr) -1L, true, 0);
  203.     return(myWindow);
  204. }
  205.  
  206.  
  207. //////////
  208. //
  209. // HandleApplicationMenu
  210. // Handle selections in the application's menus.
  211. //
  212. //////////
  213.  
  214. void HandleApplicationMenu (short theMenuID, short theMenuItem)
  215. {
  216.     MovieController     myMC = NULL;
  217.     
  218.     switch (theMenuID) {
  219.         case mTesting:
  220.             switch(theMenuItem) {
  221.                 case iTest1:
  222.                     // @@@INSERT YOUR TEST FUNCTION 1 HERE
  223.                     myMC = GetMCFromFrontWindow();
  224.                     if (myMC != NULL) {
  225.                         QTVRUtils_HideControllerBar(myMC);
  226.                     }
  227.                     break;
  228.                 
  229.                 case iTest2:
  230.                     // @@@INSERT YOUR TEST FUNCTION 2 HERE
  231.                     myMC = GetMCFromFrontWindow();
  232.                     if (myMC != NULL) {
  233.                         QTVRUtils_ShowControllerBar(myMC);
  234.                     }
  235.                     break;
  236.                 
  237.                 case iTest3: {
  238.                     // @@@INSERT YOUR TEST FUNCTION 3 HERE
  239.                     QTVRInstance    myInstance;
  240.                     
  241.                     myInstance = GetQTVRInstanceFromFrontWindow();
  242.                     MyTestFunc(myInstance);
  243.                     break;
  244.                 }
  245.             }
  246.             break;
  247.     }
  248. }
  249.  
  250.  
  251. //////////
  252. //
  253. // AdjustApplicationMenus
  254. // Adjust state of items in the application's menus.
  255. //
  256. //////////
  257.  
  258. void AdjustApplicationMenus (void)
  259. {
  260.     WindowRef             myWindow = NULL;
  261.     MovieController     myMC = NULL;
  262.     
  263.     myMC = GetMCFromFrontWindow();
  264.     if (myMC != NULL) {
  265.         EnableItem(GetMHandle(mTesting), iTest1);
  266.         EnableItem(GetMHandle(mTesting), iTest2);
  267.         EnableItem(GetMHandle(mTesting), iTest3);
  268.     } else {
  269.         DisableItem(GetMHandle(mTesting), iTest1);
  270.         DisableItem(GetMHandle(mTesting), iTest2);
  271.         DisableItem(GetMHandle(mTesting), iTest3);
  272.     }
  273.     
  274.     DisableItem(GetMHandle(mFile), iNew);        // we don't allow creating new VR files here...
  275. }
  276.  
  277.  
  278. //////////
  279. //
  280. // AddControllerFunctionality
  281. // Configure the movie controller.
  282. //
  283. //////////
  284.  
  285. void AddControllerFunctionality (MovieController theMC)
  286. {
  287.     long            myControllerFlags;
  288.     
  289.     // CLUT table use    
  290.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  291.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  292.  
  293.     // enable keyboard event handling    
  294.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  295.     
  296.     // disable drag support
  297.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  298. }
  299.  
  300.  
  301. //////////
  302. //
  303. // GetQTVRInstanceFromFrontWindow
  304. // Get the QTVRInstance associated with the front window.
  305. //
  306. //////////
  307.  
  308. QTVRInstance GetQTVRInstanceFromFrontWindow (void)
  309. {
  310.     QTVRInstance         myInstance = NULL;
  311.     WindowRef             myWindow = NULL;
  312.     WindowObject        myWindowObject = NULL;
  313.  
  314.     myWindow = FrontWindow();
  315.     if (myWindow == NULL)
  316.         return(NULL);
  317.  
  318.     if (!IsAppWindow(myWindow))
  319.         return(NULL);
  320.             
  321.     myWindowObject = (WindowObject)GetWRefCon(myWindow);
  322.     if (myWindowObject == NULL)
  323.         return(NULL);
  324.         
  325.     MoveHHi((Handle)myWindowObject);
  326.     HLock((Handle)myWindowObject);
  327.  
  328.     // test if this is indeed a movie controller and not an otherwise valid pointer (non-NULL value)
  329.     if (!IsWindowObjectOurs(myWindowObject))
  330.         return(NULL);
  331.         
  332.     myInstance = (**myWindowObject).fInstance;
  333.     HUnlock((Handle)myWindowObject);
  334.     
  335.     return(myInstance);
  336. }
  337.  
  338.  
  339. //////////
  340. //
  341. // InitApplicationWindowObject
  342. // Do any application-specific initialization of the window object.
  343. //
  344. //////////
  345.  
  346. void InitApplicationWindowObject (WindowObject theWindowObject)
  347. {
  348.     Track                myQTVRTrack = NULL;
  349.     OSErr                myErr = noErr;
  350.     Movie                myMovie = NULL;
  351.     MovieController        myMC = NULL;
  352.     QTVRInstance        myInstance = NULL;
  353.         
  354.     if (theWindowObject == NULL)
  355.         return;
  356.  
  357.     // find the QTVR Track
  358.     myMovie = (**theWindowObject).fMovie;
  359.     myMC = (**theWindowObject).fController;
  360.     myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
  361.     
  362.     myErr = QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
  363.     (**theWindowObject).fInstance = myInstance;
  364.  
  365.     // do any app-specific window configuration
  366.     if (myInstance != NULL) {
  367.     
  368.         // set unit to radians
  369.         myErr = QTVRSetAngularUnits(myInstance, kQTVRRadians);
  370.                 
  371.         // do any 3D sound-specific configuration
  372.         if (gHasSoundSprockets) {
  373.             ApplicationDataHdl    myAppData;
  374.             
  375.             // do SoundSprocket window configuration
  376.             myAppData = VR3DSound_InitWindowData(theWindowObject);
  377.             (**theWindowObject).fAppData = (Handle)myAppData;
  378.             
  379.             // install node-entering and node-leaving procedures
  380.             myErr = QTVRSetEnteringNodeProc(myInstance, NewEnteringNodeProc(VR3DSound_EnteringNodeProc), (SInt32)theWindowObject, 0);
  381.             myErr = QTVRSetLeavingNodeProc(myInstance, NewEnteringNodeProc(VR3DSound_LeavingNodeProc), (SInt32)theWindowObject, 0);
  382.             
  383.             // install an intercept procedure and a prescreen procedure to handle pan and tilt
  384.             VR3DSound_InstallInterceptRoutine(myInstance, theWindowObject);
  385.             VR3DSound_InstallPrescreenRoutine(myInstance, theWindowObject);
  386.             
  387.             // initialize orientation to match initial pan/tilt of VR movie
  388.             VR3DSound_PrescreenRoutine(myInstance, theWindowObject);
  389.             
  390.             // initialize 3D sound for first node
  391.             VR3DSound_EnteringNodeProc(myInstance, 0, theWindowObject);
  392.         }            
  393.     }
  394. }
  395.  
  396.  
  397. //////////
  398. //
  399. // RemoveApplicationWindowObject
  400. // Do any application-specific clean-up of the window object.
  401. //
  402. //////////
  403.  
  404. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  405. {
  406.     OSErr                myErr = noErr;
  407.     short                myIndex;
  408.     ApplicationDataHdl    myAppData;
  409.     
  410.     if (theWindowObject == NULL)
  411.         return;
  412.         
  413.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(theWindowObject);
  414.     if (myAppData == NULL)
  415.         return;
  416.             
  417.     // shut down 3D sound capabilities, if enabled
  418.     if (gHasSoundSprockets) {
  419.     
  420.         VR3DSound_StopNodeSounds(theWindowObject);
  421.         VR3DSound_DumpNodeSounds(theWindowObject);    
  422.         
  423.         for (myIndex = 0; myIndex < kMaxNumSourcesPerNode; myIndex++) {
  424.             SSpSource_Dispose((**myAppData).fSources[myIndex]);
  425.             SndDisposeChannel((**myAppData).fChannels[myIndex], true);
  426.         }
  427.         
  428.         SSpListener_Dispose((**myAppData).fListener);
  429.     }
  430.  
  431.     // DoDestroyMovieWindow in MacFramework.c releases the window object itself
  432. }
  433.  
  434.  
  435. //////////
  436. //
  437. // ApplicationMCActionFilterProc 
  438. // Intercept some mc actions for the QuickTime VR movie controller.
  439. //
  440. //////////
  441.  
  442. pascal Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, WindowRef theWindow)
  443. {
  444. #pragma unused(theParams)
  445.  
  446.     Boolean        isHandled = false;
  447.     
  448.     switch (theAction) {
  449.     
  450.         // handle window resizing
  451.         case mcActionControllerSizeChanged: {
  452.             Rect                myMovieBounds;
  453.  
  454.             MCGetControllerBoundsRect(theMC, &myMovieBounds);
  455.             SizeWindow((WindowPtr)theWindow, myMovieBounds.right - myMovieBounds.left,
  456.                                              myMovieBounds.bottom - myMovieBounds.top, true);
  457.             break;
  458.         }
  459.                         
  460.         default:
  461.             break;
  462.     }
  463.     
  464.     return(isHandled);    
  465. }
  466.